package com.itheima.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Member;
import com.itheima.utils.RSAUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;
import redis.clients.jedis.JedisPool;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AuthFilter implements Filter {

    private ServletContext servletContext;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        servletContext = filterConfig.getServletContext();//给servletContext初始化赋值！

    }

    JedisPool jedisPool;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        if(jedisPool == null){
            WebApplicationContext applicationContext =
                    (WebApplicationContext) servletContext.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springmvc");
            jedisPool = applicationContext.getBean(JedisPool.class);
        }


        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        // 不登录也可以访问的资源。需要直接放行
        String requestURI = httpServletRequest.getRequestURI();
        //放行一些页面  比如：登录页 js css 图片
        if(requestURI.equals("/pages/login.html")
                ||requestURI.equals("/pages/index.html")
                ||requestURI.equals("/member/login.do")
                ||requestURI.equals("/order/submit.do")
                ||requestURI.contains("/validateCode/")
                ||requestURI.contains("/setmeal/")
                ||requestURI.equals("/pages/m_setmeal.html")
                ||requestURI.equals("/pages/setmeal.html")
                ||requestURI.contains("/pages/setmeal_detail")
                ||requestURI.equals("/pages/orderInfo.html")
                ||requestURI.equals("/address/findAll.do")
                ||requestURI.equals("/")){
            filterChain.doFilter(servletRequest,servletResponse);//放行
            return;
        }

        // css  js   图片 这些也需要直接放行

        // 放行静态资源
        if(requestURI.contains("/fonts/")
                ||requestURI.contains("/css/")
                ||requestURI.contains("/img/")
                ||requestURI.contains("/plugins")){
            filterChain.doFilter(servletRequest,servletResponse);//放行
            return;
        }






        // 判断用户是否登录，如果没有登录，跳转到登录页。如果登陆了则放行。
        Cookie[] cookies = httpServletRequest.getCookies();
        if(cookies!=null){
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if("login_member_telephone".equals(name)){
                    String telephone = cookie.getValue();
                    telephone =  new String(RSAUtils.decodeBase64(telephone));
                    // 从数据库中获取登录成功时保存的用户信息
                    String json = jedisPool.getResource().get(telephone);
                    // Fastjson  java对象 ---> JSON字符串
                    // JSON.toJSONString(obj);

                    //  JSON字符串----> java对象
                    if(!StringUtils.isEmpty(json)){
                        Member member = JSON.parseObject(json, Member.class);
                        if(telephone.equals(member.getPhoneNumber())){//登录过
                            filterChain.doFilter(servletRequest,servletResponse);//放行
                            return;
                        }
                    }
                }
            }
        }

        //没有登录  跳转都登录页
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        httpServletResponse.sendRedirect("/pages/login.html");

    }

    @Override
    public void destroy() {

    }
}
